package com.yvan.codeslave.action;

import com.google.common.collect.Lists;
import com.intellij.database.psi.DbColumn;
import com.intellij.database.psi.DbTable;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.yvan.codeslave.PluginUtils;
import com.yvan.codeslave.snippet.SnippetService;
import lombok.val;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class DatabasePopActions extends ActionGroup {


    /**
     * 是否不存在子菜单
     */
    private boolean notExistsChildren;

    /**
     * 是否分组按钮
     *
     * @return 是否隐藏
     */
    @Override
    public boolean hideIfNoVisibleChildren() {
        return this.notExistsChildren;
    }

    @NotNull
    @Override
    public AnAction[] getChildren(@Nullable AnActionEvent event) {
        // 获取当前项目
        Project project = getEventProject(event);
        if (project == null) {
            return getEmptyAnAction();
        }

        //获取选中的PSI元素
        PsiElement psiElement = event.getData(LangDataKeys.PSI_ELEMENT);
        if (psiElement instanceof DbTable) {
            PluginUtils.dbTable = (DbTable) psiElement;
        }
        if (psiElement instanceof DbColumn) {
            PluginUtils.dbColumn = (DbColumn) psiElement;
        }

        //获取选中的所有表
        PsiElement[] psiElements = event.getData(LangDataKeys.PSI_ELEMENT_ARRAY);
        List<DbTable> dbTableList = Lists.newArrayList();
        List<DbColumn> dbColumnList = Lists.newArrayList();
        for (PsiElement element : psiElements) {
            if (element instanceof DbTable) {
                dbTableList.add((DbTable) element);
            }
            if (element instanceof DbColumn) {
                dbColumnList.add((DbColumn) element);
            }
        }
        if (dbTableList.isEmpty() && dbColumnList.isEmpty()) {
            return getEmptyAnAction();
        }

        //保存数据到缓存
        PluginUtils.dbTableList = dbTableList;
        PluginUtils.dbColumnList = dbColumnList;
        this.notExistsChildren = false;
        return getMenuList();
    }

    /**
     * 初始化注册子菜单项目
     *
     * @return 子菜单数组
     */
    private AnAction[] getMenuList() {
        List<AnAction> actionList = Lists.newArrayList(new ReloadAction());
        val menus = SnippetService.getTemplates();

        for (val entry : menus.entrySet()) {
            actionList.add(new SnippetAction(entry.getKey()));
        }

        val ret = new AnAction[actionList.size()];
        actionList.toArray(ret);

        return ret;
        //return new AnAction[]{
        //        new BuildModuleAction(),
        //        new CopyEntityAction(),
        //        new CopySqlAction()
        //};
    }

    /**
     * 获取空菜单组
     *
     * @return 空菜单组
     */
    private AnAction[] getEmptyAnAction() {
        this.notExistsChildren = true;
        return AnAction.EMPTY_ARRAY;
    }
}
